import {
	Controller,
	Get,
	Post,
	Body,
	Patch,
	Param,
	Delete,
	ClassSerializerInterceptor,
	UseInterceptors,
	UseGuards,
	Req,
	Query
} from "@nestjs/common";
import { UsersService } from "./users.service";
import { CreateUserDto } from "./dto/create-user.dto";
import { UpdateUserDto, UpdateUserPassWordDto } from "./dto/update-user.dto";
import { ApiBearerAuth, ApiOperation, ApiResponse, ApiTags } from "@nestjs/swagger";
import { Users } from "./entities/user.entity";
import { Roles, RolesGuard } from "../auth/role.guard";
import { QueryUserDto } from "./dto/find-user.dto";
import { JwtAuthGuard } from "../auth/jwt-auth.guard";

@ApiTags("用户")
@Controller("users")
export class UsersController {
	constructor(private readonly usersService: UsersService) {}

	@ApiOperation({ summary: "注册用户" })
	@ApiResponse({ type: [Users] })
	@UseInterceptors(ClassSerializerInterceptor)
	@Post()
	create(@Body() createUserDto: CreateUserDto) {
		return this.usersService.create(createUserDto);
	}

	@ApiOperation({ summary: "返回用户列表" })
	@ApiResponse({ type: [Users] })
	@ApiBearerAuth() // swagger文档设置token
	@UseGuards(JwtAuthGuard)
	@Get()
	findAll(@Query() query: QueryUserDto) {
		return this.usersService.findAll(query);
	}

	@ApiOperation({ summary: "获取token用户信息" })
	@ApiResponse({ type: [Users] })
	@ApiBearerAuth() // swagger文档设置token
	@UseGuards(JwtAuthGuard)
	@Get("getUserInfo")
	getUserInfo(@Req() req) {
		return req.user;
	}

	@ApiOperation({ summary: "获取指定用户信息" })
	@ApiResponse({ type: [Users] })
	@ApiBearerAuth() // swagger文档设置token
	@UseGuards(JwtAuthGuard)
	@Get(":id")
	findOne(@Param("id") id: string) {
		return this.usersService.findOne(id);
	}

	@ApiOperation({ summary: "修改个人信息" })
	@ApiBearerAuth()
	@UseGuards(JwtAuthGuard)
	@Patch("/updateMyInfo")
	updateMyInfo(@Req() req, @Body() updateUserDto: UpdateUserDto) {
		return this.usersService.update(req.user.id, updateUserDto);
	}

	@ApiOperation({ summary: "修改人员信息" })
	@ApiBearerAuth()
	@Roles("root")
	@UseGuards(JwtAuthGuard, RolesGuard)
	@Patch(":id")
	update(@Param("id") id: string, @Body() updateUserDto: UpdateUserDto) {
		return this.usersService.update(id, updateUserDto);
	}

	@ApiOperation({ summary: "密码重置" })
	@ApiBearerAuth()
	@Roles("root")
	@UseGuards(JwtAuthGuard, RolesGuard)
	@Patch("/resetPassword/:id")
	resetPassword(@Param("id") id: string) {
		return this.usersService.resetPassword(id);
	}

	@ApiOperation({ summary: "修改密码" })
	@ApiBearerAuth()
	@UseGuards(JwtAuthGuard)
	@Patch("/updatePassword/:id")
	updatePassword(@Req() req, @Body() updateUserPassWordDto: UpdateUserPassWordDto) {
		return this.usersService.updatePassword(
			req.user.id,
			updateUserPassWordDto.oldPassWord,
			updateUserPassWordDto.newPassWord
		);
	}

	@ApiOperation({ summary: "删除人员信息" })
	@ApiBearerAuth()
	@Roles("root")
	@UseGuards(JwtAuthGuard, RolesGuard)
	@Delete(":id")
	remove(@Param("id") id: string) {
		return this.usersService.remove(id);
	}
}
